/**
 * 日期格式化
 * @param startDate
 * @param endDate
 * @param dataFormat
 */
function initLayerDate(startDate, endDate, option) {
	//时间控件
	foolInitLayerDate(startDate, endDate, option);
}

function foolInitLayerDate(startDate, endDate, option) {
	initLayerDate2("." + startDate, "." + endDate, option);
}

/**
 * 根据startDateId, endDateId查询对应的标签,并将其渲染为日期控件。
 * @param 
 * @return 
 * @author cjw 2020-04-03
 */
function initLayerDate_2(startDateId, endDateId, option) {
	initLayerDate2("#" + startDateId, "#" + endDateId, option);
}

function initLayerDate2(startElem, endElem, option) {
	option = option || {};
	//时间控件
	var defaults = {
		type: 'date',
	};
	layui.use('laydate', function () {
		var laydate = layui.laydate;
		var endDate;
		var startDate;
		lay(startElem).each(function () {
			startDate = laydate.render($.extend(defaults, {
				elem: this,
				done: function (value, dates) {
					if (endDate) {
						endDate.config.min = {
							year: dates.year,
							month: dates.month - 1, //关键
							date: dates.date,
							hours: dates.hours,
							minutes: dates.minutes,
							seconds: dates.seconds
						}
					}
					;
					if (option._doneEvent instanceof Function) {
						option._doneEvent(value);
					}
				}
			}, option));
		});
		if (endElem) {
			endDate = laydate.render($.extend(defaults, {
				elem: endElem
				, done: function (value, dates) {
					startDate.config.max = {
						year: dates.year,
						month: dates.month - 1,//关键
						date: dates.date,
						hours: dates.hours,
						minutes: dates.minutes,
						seconds: dates.seconds
					}
					if (option._doneEvent instanceof Function) {
						option._doneEvent();
					}
				}
			}, option));
		}
	});
	
}


function __getDateFromElement(e) {
	e = $(e);
	var str;
	if (e.is("input")) {
		str = e.val();
	} else {
		str = e.text();
	}
	if (!str) {
		return null;
	}
	str = (str).replace(/-/g, "/");
	return new Date(str);
}

function __getLaydateBound(dates) {
	return {
		year: dates.getFullYear(),
		month: dates.getMonth(),//关键
		date: dates.getDate(),
		hours: dates.getHours(),
		minutes: dates.getMinutes(),
		seconds: dates.getSeconds()
	}
}

function __getLaydateBoundFromElement(e, format) {
	if (format == "HH:mm:ss") {
		e = $(e);
		var str;
		if (e.is("input")) {
			str = e.val();
		} else {
			str = e.text();
		}
		if (!str) {
			return null;
		}
		var arr = str.split(":");
		for (var i = 0; i < arr.length; i++) {
			arr[i] = parseInt(arr[i]);
		}
		var now = new Date();
		return {
			year: now.getFullYear(),
			month: now.getMonth(),//关键
			date: now.getDate(),
			hours: arr[0],
			minutes: arr[1],
			seconds: arr[2]
		};
	}
	var date = __getDateFromElement(e);
	if (date == null) {
		return null;
	}
	return __getLaydateBound(date);
}

function timerangeGroup(group, start, end, option) {
	
	var defaults = {
		type: 'date',
	};
	option = $.extend({}, defaults, option);
	
	
	if (window.__TIMERANGE_GROUP_HAS_BIND) {
		$(document).unbind("mouseover", group);
	}
	window.__TIMERANGE_GROUP_HAS_BIND = true;
	
	$(document).on("mouseover", group, function () {
		(function (_group, _start, _end) {
			
			var _start = _group.find(start);
			var _end = end ? _group.find(end) : null;
			layui.use('laydate', function () {
				var laydate = layui.laydate;
				
				var _option = $.extend({}, {
					elem: _start[0], trigger: 'click',
					
					__ext_on_render: function (date) {
						if (!_end) {
							return;
						}
						var max = __getLaydateBoundFromElement(_end, this.format);
						console.log(this, date, max);
						if (!max) {
							return;
						}
						this.max = max;
					}
				}, option);
				laydate.render(_option);
			});
			if (!_end) {
				return;
			}
			layui.use('laydate', function () {
				var laydate = layui.laydate;
				
				var _option = $.extend({}, {
					elem: _end[0], trigger: 'click',
					__ext_on_render: function (date) {
						var min = __getLaydateBoundFromElement(_start, this.format);
						console.log(this, date, min);
						if (!min) {
							return;
						}
						this.min = min;
					}
				}, option);
				laydate.render(_option);
			});
		})($(this));
		
	});
}